package com.xuecheng.content.controller;

import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.TeachplanDto;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.TeachplanVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.service.CourseBaseService;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 课程计划前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class TeachplanController {

    @Autowired
    private TeachplanService  teachplanService;
    // 查询课程计划
    @GetMapping("/teachplan/{courseId}/tree-nodes")
    public ResponseResult<TeachplanVo> treeNodes(@PathVariable("courseId") Long courseId) {
        return teachplanService.treeNodes(courseId);
    }
    // 新增或修改课程计划
    @PostMapping("/teachplan")
    public ResponseResult saveOrUpdateTeachplan(@RequestBody TeachplanDto dto) {
        return teachplanService.saveOrUpdateTeachplan(dto);
    }
    // 删除课程计划
    @DeleteMapping("/teachplan/{id}")
    public ResponseResult deleteTeachplan(@PathVariable("id") Long teachplanId) {
        return teachplanService.deleteTeachplan(teachplanId);
    }

    @Autowired
    private CourseBaseService courseBaseService;
    // 提交审核
    @GetMapping("/course/commit/{courseBaseId}")
    public ResponseResult commitCourseBase(@PathVariable("courseBaseId")Long courseBaseId) {
        courseBaseService.lambdaUpdate()
                .set(CourseBase::getAuditStatus, CourseConstants.AUDIT_COMMIT_STATUS)
                .eq(CourseBase::getId, courseBaseId).update();
        return ResponseResult.okResult();
    }
    // 课程审核
    @PostMapping("/course/approve")
    public ResponseResult approve(@RequestBody CourseAuditDto dto) {
        courseBaseService.lambdaUpdate()
                .set(CourseBase::getAuditStatus, dto.getAuditStatus())
                .set(CourseBase::getAuditMind, dto.getAuditMind())
                .eq(CourseBase::getId, dto.getCourseBaseId()).update();

        return ResponseResult.okResult();
    }
    // 根据id查询课程计划
    @GetMapping("/teachplan/{teachplanId}")
    public ResponseResult<TeachplanVo> findTechplanById(@PathVariable("teachplanId")Long teachplanId){
        // 1.直接查询
        Teachplan teachplan = teachplanService.getById(teachplanId);
        // 2.转vo
        TeachplanVo vo = BeanHelper.copyProperties(teachplan, TeachplanVo.class);
        // 3.返回
        return ResponseResult.okResult(vo);
    }
}
